package com.qgzj.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qgzj.common.vo.CatalogRequest;
import com.qgzj.common.vo.VolumeRequest;
import com.qgzj.entity.Catalog;
import com.qgzj.entity.Volume;
import com.qgzj.mapper.CatalogMapper;
import com.qgzj.response.PageQueryRes;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CatalogService {
    private static Logger logger = LoggerFactory.getLogger(CatalogService.class);

    @Resource
    CatalogMapper catalogMapper;

    public PageQueryRes<Catalog> selectCatalog (CatalogRequest catalogRequest){
        int page = catalogRequest.getPage();
        int pageSize = catalogRequest.getPageSize();
        PageQueryRes res = new PageQueryRes();
        res.setPage(page);
        res.setPageSize(pageSize);
        String materialNo = catalogRequest.getMaterialNo();
        String volumeNo = catalogRequest.getVolumeNo();
        EntityWrapper<Catalog> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        wrapper.eq("volume_no", volumeNo);
        wrapper.orderBy("id",false);
        List<Catalog> catalogs = catalogMapper.selectPage(new Page<>(page, pageSize),wrapper);
        res.setCount(catalogMapper.selectCount(wrapper));
        res.setList(catalogs);
        return res;
    }


    public void insertCatalog(CatalogRequest catalogReq) {
        Catalog catalog = transCatalogReq2Catalog(catalogReq);
        catalog.setCreateTime(new Date());
        catalog.setUpdateTime(new Date());
//        if(StringUtils.isBlank(catalogReq.get)){
//
//        }
        Integer insert = catalogMapper.insert(catalog);
    }

    public void updateCatalog(CatalogRequest catalogReq) {
        Catalog catalog = transCatalogReq2Catalog(catalogReq);
        catalog.setUpdateTime(new Date());
        String materialNo = catalog.getMaterialNo();
        String volumeNo = catalog.getVolumeNo();
        String name = catalog.getName();
        EntityWrapper<Catalog> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("name", name);
        Integer update = catalogMapper.update(catalog, wrapper);
    }

    public void deleteCatalog(CatalogRequest catalog) {
        String materialNo = catalog.getMaterialNo();
        String volumeNo = catalog.getVolumeNo();
        String name = catalog.getName();
        EntityWrapper<Catalog> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        wrapper.eq("volume_no", volumeNo);
        wrapper.eq("name", name);
        Integer update = catalogMapper.delete(wrapper);
    }

    public Catalog transCatalogReq2Catalog(CatalogRequest catalogReq){
        Catalog catalog = new Catalog();
//        if(StringUtils.isNotBlank(catalogReq.getName())){
//            catalog.setName(catalogReq.getName());
//        }
//        if(StringUtils.isNotBlank(catalogReq.getVolumeNo())){
//            catalog.setVolumeNo(catalogReq.getVolumeNo());
//        }
//        if(StringUtils.isNotBlank(catalogReq.getMaterialNo())){
//            catalog.setMaterialNo(catalogReq.getMaterialNo());
//        }
//        if(null!=catalogReq.getStartPage()){
//            catalog.setStartPage(catalogReq.getStartPage());
//        }
//        catalog.setCreateTime(new Date());
//        catalog.setUpdateTime(new Date());
        BeanUtils.copyProperties(catalogReq,catalog);
        return catalog;
    }



}
